package com.pagepub.core.tag.validator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

public class ValidateTelephone extends FormValidateBasic {
	/**
	 * 
	 */
	private static final long serialVersionUID = 825552544434978601L;
	/**
	 * @param args
	 */
	
	private String submitvalidate;
    private String rightimage;
    private String rightwordcolor;  
	public String getRightimage() {
		return rightimage;
	}
	public void setRightimage(String rightimage) {
		this.rightimage = rightimage;
	}
	public String getRightwordcolor() {
		return rightwordcolor;
	}
	public void setRightwordcolor(String rightwordcolor) {
		this.rightwordcolor = rightwordcolor;
	}
	public String getSubmitvalidate() {
		return submitvalidate;
	}
	public void setSubmitvalidate(String submitvalidate) {
		this.submitvalidate = submitvalidate;
	}
	public int doStartTag() throws JspException {
		JspWriter out = (JspWriter) this.pageContext.getOut();   
	//	validatetelephone();

		try {
			 out.print(validatetelephone());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return 1;
	}
	public static void main(String[] args) {
		// TODO 自动生成方法存根

	}
	
	public StringBuffer validatetelephone(){
		StringBuffer str=new StringBuffer();
		String emptyelement="";
	     String pluselement="";
	      if(this.submitvalidate.equalsIgnoreCase("true")){
				emptyelement="emptyelement(realname);";
				pluselement="else pluselement(realname);";
			}
		str.append("<script language=\"javascript\">");
		str.append("function validatetelephone(realvalue,min,max,spanname,realname) {");	
		str.append("if(realvalue!=\"\")");
		str.append("if( validatetelephoneformat(realvalue,spanname,realname))");
		str.append("validatelengthdisplayrightmessage(realvalue,min,max,spanname,realname);");		
		str.append(pluselement);
		 str.append("else {");
		   this.displayempty();
		   str.append(emptyelement);	   
		   str.append("}");
		str.append("}");
		str.append("</script>");
		str.append(this.validatetelephoneformat());
		str.append(this.returnfunction());
		return str;
	}
	public StringBuffer validatetelephoneformat(){
		StringBuffer str=new StringBuffer();
		str.append("<script language=\"javascript\">");
		str.append("function validatetelephoneformat(realvalue,spanname,realname){");
		str.append("var flag=0;");
		str.append("var reg=/(^[0-9]{3,4}\\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\\([0-9]{3,4}\\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;");
		str.append("if(realvalue!=\"\"){");
		str.append(" if (reg.test(realvalue)==false)");
		str.append("			{");
		str.append("				message=\"对不起,\"+realname+\"不是合法号码!\";	");	
		str.append("				imgurl=\"<img src=\\\""+wrongimage+"\\\"/>\";");
		str.append(this.display());
		str.append("return false;");				
		str.append("			}");
		str.append("else {");
		str.append(this.displayempty());
		str.append("return true;}");

		str.append("		}");
		str.append("}");
		str.append("</script>");
		return str;
	}
	 public StringBuffer returnfunction(){
		  StringBuffer string=new StringBuffer();
		  ValidateLengthDisplayRightMessage message=new ValidateLengthDisplayRightMessage();
		  message.setRightimage(rightimage);
		  message.setRightwordcolor(rightwordcolor);
		  message.setSubmitvalidate(submitvalidate);
		  message.setWordcolor(wordcolor);
		  message.setWrongimage(wrongimage);
		  string.append(message.validatelengthdisplayrightmessage());
		  return string;
	  }
}
